import { $wuxSelect } from '../../component/wux/index'
var common = require('../../lib/common.js');
var url = require('../../config/url.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 上传链接
        uploadUrl: url.uploadImage,
        // 名称
        name: '',
        // 简介
        desc: '',
        // 账号类型
        account_type: {
            selected: 0,
            name: [
                '个体', '店铺', '企业'
            ],
            tag: [
                'individual', 'shop', 'company'
            ]
        },

        // 圈子信息
        circle: {},

        // logo
        logo: [],

        // 产品
        products: [
            {
                onetype: {
                    selected: 0,
                    value: '',
                    name: [
                    ],
                    tag: [
                    ]
                },
                twotype: {
                    selected: [0],
                    value: '',
                    name: [
                    ],
                    tag: [
                    ]
                },
            }
        ],

        // 轮播图
        slideshowList: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getCircleInfo();
        this.checkUserInfo();
    },

    /**
     * 账号类型选择
     */
    onAccountTypeSelect() {
        $wuxSelect('#account_type').open({
            value: this.data.account_type.name[this.data.account_type.selected],
            options: this.data.account_type.name,
            onConfirm: (value, index, options) => {
                let account_type = this.data.account_type;
                account_type.selected = index;
                this.setData({
                    account_type,
                })
            },
        })
    },

    checkUserInfo() {
        common.post(url.getUserInfo, {}, (data) => {
            this.setData({ user: data });
            if (data.mobile == 0) {
                wx.showModal({
                    title: '温馨提示',
                    content: '您的个人信息暂未完善，请先完善',
                    confirmText: '马上完善',
                    success(res) {
                        if (res.confirm) {
                            wx.redirectTo({
                                url: '/pages/user_update/index'
                            })
                        } else if (res.cancel) {
                            wx.navigateBack({
                                delta: 1
                            })
                        }
                    }
                })
            }
        });
    },

    /**
     * 新增产品
     */
    onAddProduct() {
        let products = this.data.products;
        let product = {
            onetype: {
                value: products[0].onetype.name[0],
                selected: 0,
                name: products[0].onetype.name,
                tag: products[0].onetype.tag
            },
            twotype: {
                value: '',
                selected: [],
                name: [],
                tag: []
            }
        };

        products.push(product);
        this.setData({ products });

        this.getTwoType(products.length - 1, product.onetype.tag[0]);
    },

    /**
     * 获取第一类
     */
    getOneType(index, reset) {
        wx.showLoading();
        common.post(url.getCircleOneType, {}, (data) => {
            wx.hideLoading();
            let products = this.data.products;
            let product = products[index];
            let onetype = product.onetype;

            if (reset) {
                onetype.selected = 0;
                onetype.tag = [];
            }
            onetype.name = [];
            onetype.value = data[0].name;

            for (let i in data) {
                onetype.name.push(data[i].name);
                onetype.tag.push(data[i].id);
            }

            product.onetype = onetype;
            products[index] = product;

            this.setData({ products });
            this.getTwoType(index, onetype.tag[onetype.selected], reset);
        });
    },

    /**
     * 获取第二类
     * @param {*} index 
     * @param {*} oneid 
     */
    getTwoType(index, oneid, reset) {
        wx.showLoading();
        common.post(url.getCircleTwoType, { id: oneid }, (data) => {
            wx.hideLoading();
            let products = this.data.products;
            let product = products[index];
            let twotype = product.twotype;
            if (reset) {
                twotype.selected = [];
                twotype.value = '';
            }

            twotype.tag = [];
            twotype.name = [];

            for (let i in data) {
                twotype.name.push(data[i].name);
                twotype.tag.push(data[i].id);
            }

            product.twotype = twotype;
            products[index] = product;

            this.setData({ products });
        });
    },

    /**
     * 删除产品
     */
    onDelProduct() {
        let products = this.data.products;
        products.pop();
        this.setData({ products });
    },

    /**
     * 产品第一类选择
     */
    onProductOneSelect({ currentTarget }) {
        let dataset = currentTarget.dataset;
        let index = dataset.index;

        let product = this.data.products[index];
        let onetype = product.onetype;

        $wuxSelect('#products_one_' + index).open({
            value: onetype.name[onetype.selected],
            options: onetype.name,
            onConfirm: (value, selected, options) => {
                let products = this.data.products;
                onetype.selected = selected;
                onetype.value = onetype.name[selected];
                product.onetype = onetype;
                products[index] = product;
                this.setData({
                    products,
                });

                this.getTwoType(index, onetype.tag[selected], true);
            },
        })
    },


    /**
     * 产品第二类选择
     */
    onProductTwoSelect({ currentTarget }) {
        let dataset = currentTarget.dataset;
        let index = dataset.index;

        let product = this.data.products[index];
        let twotype = product.twotype;

        $wuxSelect('#products_two_' + index).open({
            multiple: true,
            value: twotype.selected.map(n => {
                return twotype.name[n];
            }),
            options: twotype.name,
            onConfirm: (value, selected, options) => {
                let products = this.data.products;
                twotype.selected = selected;
                twotype.value = selected.map(n => {
                    return twotype.name[n];
                });
                product.twotype = twotype;
                products[index] = product;
                this.setData({
                    products,
                });
            },
        })
    },

    /**
     * logo改变
     * @param {*} e 
     */
    onLogoChange(e) {
        const { file, fileList } = e.detail
        if (file.status === 'uploading') {
            wx.showLoading({ mask: true });
        } else if (file.status === 'done' || file.status == 'remove') {
            this.setData({
                logo: fileList,
            });
        }
    },

    /**
     * 图片上传完成
     * @param {*} e 
     */
    onImageComplete(e) {
        wx.hideLoading();
    },

    /**
     * 图片查看
     * @param {*} e 
     */
    onImagePreview(e) {
        const { file, fileList } = e.detail;
        wx.previewImage({
            current: file.url,
            urls: fileList.map((n) => n.url),
        })
    },

    /**
     * 轮播图改变
     * @param {*} e 
     */
    onSlideshowChange(e) {
        const { file, fileList } = e.detail
        if (file.status === 'uploading') {
            wx.showLoading({ mask: true });
        } else if (file.status === 'done' || file.status == 'remove') {
            this.setData({ slideshowList: fileList });
        }
    },

    /**
     * 内容变化
     * @param {*} e 
     */
    onChangeValue(e) {
        let name = e.currentTarget.dataset.name;
        let value = e.detail.detail.value;
        let a = {};
        a[name] = value;
        this.setData(a);
    },

    /**
     * 加入
     */
    onJoin() {
        let params = {
            name: this.data.name,
            desc: this.data.desc,
        }
        if (this.data.logo.length == 0) {
            wx.showModal({
                title: '提示',
                content: '请上传Logo',
                success(res) {
                }
            })
            return false;
        }

        // Logo
        let logo = this.data.logo[0];
        if (logo.res) {
            params.logo = JSON.parse(logo.res.data).data.url;
        } else {
            params.logo = logo.url;
        }

        // 图片
        let images = [];
        images = this.data.slideshowList.map((n) => {
            if (n.res) {
                let data = JSON.parse(n.res.data);
                return data.data.url;
            } else {
                return n.url
            }
        });
        params.slideshows = images;

        // 类型
        params.type = [];
        for (let i in this.data.products) {
            let element = this.data.products[i];
            let item = {
                one: element.onetype.tag[element.onetype.selected],
                two: element.twotype.selected.map(n => {
                    return element.twotype.tag[n];
                })
            }
            if (item.two.length == 0) {
                wx.showModal({
                    title: '提示',
                    content: '请选择子项',
                    success(res) {
                    }
                })
                return false;
            }
            params.type.push(item)
        }

        if (this.data.name.length < 2) {
            wx.showModal({
                title: '提示',
                content: '名称不能小于2个字',
                success(res) {
                }
            })
            return false;
        }

        wx.showLoading({ mask: true });
        params.type = JSON.stringify(params.type);
        params.slideshows = JSON.stringify(params.slideshows);
        common.post(url.addCircle, params, (data) => {
            wx.hideLoading();
            wx.showModal({
                title: '提示',
                content: '提交成功,等待管理员审核',
                success(res) {
                }
            })
        })
    },

    /**
     * 获取圈子信息
     */
    getCircleInfo() {
        common.post(url.getCircle, {}, (data) => {
            if (data) {
                let slideshowList = [];
                if (data.slideshow.length > 0) {
                    slideshowList = data.slideshow.split(',').map((url, index) => {
                        return {
                            uid: index,
                            status: 'done',
                            url: url,
                        }
                    });
                }

                let products = [];
                data.type.forEach(element => {
                    products.push({
                        onetype: {
                            selected: 0,
                            value: '',
                            name: [],
                            tag: []
                        },
                        twotype: {
                            selected: [],
                            value: [],
                            name: [],
                            tag: []
                        },
                    });
                });

                this.setData({
                    circle: data,
                    name: data.name,
                    desc: data.desc,
                    slideshowList: slideshowList,
                    logo: [
                        {
                            uid: 0,
                            status: 'done',
                            url: data.logo,
                        }
                    ],
                    products: products
                });

                data.type.forEach((element, index) => {
                    // 第一类
                    common.post(url.getCircleOneType, {}, (data1) => {
                        let products = this.data.products;
                        let product = products[index];
                        let onetype = product.onetype;
                        let twotype = product.twotype;

                        onetype.name = [];


                        for (let i = 0; i < data1.length; i++) {
                            onetype.name.push(data1[i].name);
                            onetype.tag.push(data1[i].id);
                            if (data1[i].id == element.one_type_id) {
                                onetype.selected = i;
                                onetype.value = data1[i].name;
                            }
                        }
                        product.onetype = onetype;

                        let two_ids = (element.two_type_id + '').split(',');
                        common.post(url.getCircleTwoType, { id: onetype.tag[onetype.selected] }, (data2) => {
                            twotype.tag = [];
                            twotype.name = [];
                            twotype.selected = [];
                            twotype.value = '';
                            for (let i = 0; i < data2.length; i++) {
                                twotype.name.push(data2[i].name);
                                twotype.tag.push(data2[i].id);
                                if (two_ids.indexOf(data2[i].id + '') >= 0) {
                                    twotype.selected.push(i);
                                    if (twotype.value != '') {
                                        twotype.value += ',';
                                    }
                                    twotype.value += data2[i].name;
                                }
                            }
                            product.twotype = twotype;
                            products[index] = product;

                            this.setData({ products });
                        });

                    });

                });
            } else {
                this.getOneType(0, true);
            }
        })
    },
})